package com.cskaoyan.market.service.wx.impl;

import com.cskaoyan.market.db.domain.MarketFootprint;
import com.cskaoyan.market.db.domain.MarketFootprintExample;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.mapper.MarketFootprintMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.service.wx.WxFootprintService;
import com.cskaoyan.market.vo.wx.wtz.FootprintVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wutanzi
 * @Date: 2023/5/23 下午4:17
 * @Version: v1.0.0
 * @Description: 足迹服务的实现类 分页 删除足迹 添加足迹
 **/
@Service
public class WxFootPrintServiceImpl implements WxFootprintService {
    @Autowired
    MarketFootprintMapper footprintMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    /**
     *
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<FootprintVo> list(Integer userId, Integer page, Integer limit) {
        //创建example
        MarketFootprintExample example = new MarketFootprintExample();
        //设置example的查询条件
        example.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);

        //排序
        example.setOrderByClause("update_time desc");
        //分页
        PageHelper.startPage(page, limit);
        List<MarketFootprint> pageList = footprintMapper.selectByExample(example);

        List<FootprintVo> result = new ArrayList<>();

        //遍历查询结果，创建返回值
        for (MarketFootprint footprint : pageList) {
            Integer goodsId = footprint.getGoodsId();
            MarketGoods goods = goodsMapper.selectByPrimaryKey(goodsId);
            FootprintVo footprintVo = new FootprintVo();
            footprintVo.setAddTime(footprint.getAddTime());
            footprintVo.setBrief(goods.getBrief());
            footprintVo.setGoodsId(goodsId);
            footprintVo.setId(footprint.getId());
            footprintVo.setName(goods.getName());
            footprintVo.setPicUrl(goods.getPicUrl());
            footprintVo.setRetailPrice(goods.getRetailPrice());

            result.add(footprintVo);
//            //商品id
//            Integer goodsId = footprint.getGoodsId();
//            //查询商品对象
//            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodsId);
//            if (marketGoods == null) {
//                continue;
//            }
//
//            // 创建返回值对象,加入列表
//            footprintVos.add(new FootprintVo(
//                    footprint.getUpdateTime(),
//                    marketGoods.getBrief(),
//                    marketGoods.getId(),
//                    footprint.getId(),
//                    marketGoods.getName(),
//                    marketGoods.getPicUrl(),
//                    marketGoods.getRetailPrice()));
        }
//        //创建返回值
//        CommonPageListVo<FootprintVo> result = new CommonPageListVo<>(
//                (Integer) page,
//                ((Page)pageList).getPages(),
//                limit,
//                (int)((Page)pageList).getTotal(),
//                footprintVos
//
//        );
        return result;

    }

    /**
     *  删除足迹
     * @param userId
     * @param footprintId
     * @return
     */
    @Override
    public int delete(Integer userId, Integer footprintId) {
        //获取足迹对象
        MarketFootprint footprint = footprintMapper.selectByPrimaryKey(footprintId);
        //判断足迹对象是否属于当前用户
        if(footprint == null|| !footprint.getUserId().equals(userId)){
            return 1;
        }
        //逻辑删除
        footprint.setDeleted(true);
        return footprintMapper.updateByPrimaryKeySelective(footprint);


    }

    /**
     * 添加足迹
     * @param marketFootprint
     */
    @Override
    public void addFootprint(MarketFootprint marketFootprint) {

        //获取用户id
        Integer userId = marketFootprint.getUserId();
        // 获取商品id
        Integer goodsId = marketFootprint.getGoodsId();
        //查询足迹表中是否有该用户的该商品的足迹
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = footprintExample.createCriteria();

        criteria.andUserIdEqualTo(userId).andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        List<MarketFootprint> marketFootprints = footprintMapper.selectByExample(footprintExample);
        //如果存在，且是今天的足迹，则更新
        boolean hasFlag = false;
        //遍历
        for (MarketFootprint hasFootprint : marketFootprints) {
            if (hasFootprint != null) {
                //获取存在的足迹的添加时间
                LocalDateTime addTime = hasFootprint.getAddTime();
                //与今天比较
                if (addTime.getDayOfYear() == LocalDateTime.now().getDayOfYear()) {
                    hasFlag = true;
                    hasFootprint.setUpdateTime(LocalDateTime.now());
                    footprintMapper.updateByPrimaryKeySelective(hasFootprint);
                    return;
                }
            }

        }
        //不存在，则插入
        footprintMapper.insertSelective(marketFootprint);

    }


}
